package problems;

import graph.Node;

import iddfs.IDDFS;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.GregorianCalendar;

import bfs.Bfs;

public class Experiment {

	public static void runAlgos(int numNodes, int minNumNodes, int maxNumNodes, String filename){
		PrintWriter log = null;
		File file = new File(filename);
		file.delete();
		try {
			file.createNewFile();
			log = new PrintWriter(file);
		} catch (IOException e) {
			e.printStackTrace();
		}
		double backProb = 0;
		String iddfsAns1 = minNumNodes + "\t" + maxNumNodes + "\t" + backProb;
		String iddfsAns2 = minNumNodes + "\t" + maxNumNodes + "\t" + backProb;
		String iddfsAns3 = minNumNodes + "\t" + maxNumNodes + "\t" + backProb;
		String bfsAns1 = minNumNodes + "\t" + maxNumNodes + "\t" + backProb;
		String bfsAns2 = minNumNodes + "\t" + maxNumNodes + "\t" + backProb;
		String bfsAns3 = minNumNodes + "\t" + maxNumNodes + "\t" + backProb;
		for (int i=0; i<100; i++){
			System.out.println("#"+i+" Started Generating problem (backprob="+ backProb+") - " + GregorianCalendar.getInstance().getTime());
			Problem p = new RandomProblem(minNumNodes, maxNumNodes, numNodes, backProb, minNumNodes, maxNumNodes,(numNodes - (int)(Math.random()*(double)numNodes*0.33333)));
			Node root = p.makeGraph();
			System.out.println("#"+i+" Finished Generating problem");
			System.out.println("#"+i+" BFS solving problem");
			long tStartBfs = GregorianCalendar.getInstance().getTimeInMillis();
			Bfs bfs = new Bfs();
			bfs.solve(root);
			long bfsTotalTimeMillis = GregorianCalendar.getInstance().getTimeInMillis()-tStartBfs;
			int bfsNumNodeVisits = root.reset();
			System.out.println("#"+i+" Total time for BFS: " + bfsTotalTimeMillis);
			System.out.println("#"+i+" Total nodes visited for BFS: " + bfsNumNodeVisits);
			System.out.println("#"+i+" IDDFS solving problem");
			long tStartIddfs = GregorianCalendar.getInstance().getTimeInMillis();
			IDDFS iddfs = new IDDFS();
			iddfs.solve(root);
			long iddfsTotalTimeMillis = GregorianCalendar.getInstance().getTimeInMillis()-tStartIddfs;
			int iddfsNumNodeVisits = root.reset();
			System.out.println("#"+i+" Total time for IDDFS: " + iddfsTotalTimeMillis);
			System.out.println("#"+i+" Total nodes visited for IDDFS: " + iddfsNumNodeVisits);
			System.out.println();

			bfsAns1 = bfsAns1 + "\t" + bfsTotalTimeMillis;
			iddfsAns1 = iddfsAns1 + "\t" + iddfsTotalTimeMillis;

			bfsAns2 = bfsAns2 + "\t" + bfsNumNodeVisits;
			iddfsAns2 = iddfsAns2 + "\t" + iddfsNumNodeVisits;

			bfsAns3 = bfsAns3 + "\t" + bfs.getMaxMemory();
			iddfsAns3 = iddfsAns3 + "\t" + iddfs.getMaxMemory();
		}
		log.println(bfsAns1);
		log.println(iddfsAns1);
		log.println(bfsAns2);
		log.println(iddfsAns2);
		log.println(bfsAns3);
		log.println(iddfsAns3);
		log.close();
		System.gc();
	}

	public static void main(String[] args) {
		int numNodes = 999999;
		runAlgos(numNodes, 2, 2, "2.txt");
		runAlgos(numNodes, 3, 3, "3.txt");
		runAlgos(numNodes, 4, 4, "4.txt");
		runAlgos(numNodes, 5, 5, "5.txt");
		runAlgos(numNodes, 6, 6, "6.txt");
		runAlgos(numNodes, 7, 7, "7.txt");
		runAlgos(numNodes, 8, 8, "8.txt");
		runAlgos(numNodes, 9, 9, "9.txt");
		runAlgos(numNodes, 10, 10, "10.txt");
		runAlgos(numNodes, 1, 5, "1-5.txt");
		runAlgos(numNodes, 1, 3, "1-3.txt");
	}
}

